package com.example.administrator.nl.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.administrator.nl.R;

public class DialogShoppingCartView extends Dialog  {
    private Context context;//上下文
    private int layoutResID;//布局文件id

    public DialogShoppingCartView(@NonNull Context context, int layoutResID, int style) {
        super(context,style);
        this.context = context;
        this.layoutResID = layoutResID;
    }


    public DialogShoppingCartView(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutResID);

        //提前设置Dialog的一些样式
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);//设置dialog显示在底部
//        dialogWindow.setWindowAnimations();设置动画效果

        WindowManager windowManager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x=0;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;// 宽度铺满
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;// 高度自适应
//        dialogWindow.setAttributes(lp);
        dialogWindow.setLayout(lp.width, lp.height);

        setCanceledOnTouchOutside(true);//点击外部Dialog消失

    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        getWindow().setAttributes(layoutParams);


    }
}
